package com.dt.dubai.core.aliyun.aliyunSms.service;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.dt.dubai.core.aliyun.aliyunSms.service.ser.AliyunSmsService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class AliyunSmsServiceImp implements AliyunSmsService {

    static final String accessKeyId = "LTAI4GCFPA1XJVE6JsoB6stH";
    static final String secret = "gGaSLRvFfP9M6yDQzefb2TM84T1n6t";
    static final String endpoint = "dysmsapi.aliyuncs.com";
    static final String sysVersion = "2017-05-25";
    static final String sysAction = "SendSms";
    //产品名称:云通信短信API产品,开发者无需替换
    static final String product = "Dysmsapi";
    //产品域名,开发者无需替换
    static final String domain = "dysmsapi.aliyuncs.com";

    //调用SendSms发送短信
    @Override
    public String sendSms(String phoneNumbers) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, secret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain(endpoint);//发送短信  公网服务地址（Endpoint）
        request.setSysVersion(sysVersion);//API 的版本号
        request.setSysAction(sysAction);//系统规定参数
        request.putQueryParameter("RegionId", "cn-hangzhou");//API支持的RegionID
        request.putQueryParameter("PhoneNumbers", phoneNumbers);//接收短信的手机号码
        request.putQueryParameter("SignName", "迅搭云电商平台");//短信签名名称
        request.putQueryParameter("TemplateCode", "SMS_192540281");//短信模板ID
        int rand = RandomUtil.randomInt(100000, 999999);
        String value = "{\"code\":\""+rand+"\"}";
        request.putQueryParameter("TemplateParam", value);
        String bizId = null;
        try {
            CommonResponse response = client.getCommonResponse(request);
            String resultStr = JSONObject.toJSONString(response);
            JSONObject jsonObject = JSON.parseObject(resultStr).getJSONObject("data");
            bizId = jsonObject.getString("BizId");
            System.out.println(bizId);
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return bizId;
    }

    public static QuerySendDetailsResponse querySendDetails(String bizId,String phoneNumbers) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, secret);
        DefaultProfile.addEndpoint( "cn-hangzhou", product, domain);
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象
        QuerySendDetailsRequest request = new QuerySendDetailsRequest();
        //必填-号码
        request.setPhoneNumber(phoneNumbers);
        //可选-流水号
        request.setBizId(bizId);
        //必填-发送日期 支持30天内记录查询，格式yyyyMMdd
        SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
        request.setSendDate(ft.format(new Date()));
        //必填-页大小
        request.setPageSize(10L);
        //必填-当前页码从1开始计数
        request.setCurrentPage(1L);
        //hint 此处可能会抛出异常，注意catch
        QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);
        return querySendDetailsResponse;
    }

    public static void main(String[] args) {
        AliyunSmsServiceImp aaaa = new AliyunSmsServiceImp();
        aaaa.sendSms( "15195164877");
    }

}
